package Master;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import login.UserModel;
import com.ibatis.sqlmap.client.SqlMapClient;
import ibatis.IbatisHelper;

public class SchoolManager {
	private SqlMapClient ibatis;

	public SchoolManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}

	//School
	public List selectAllSchool(String paramSchool,String paramCity){
		List result = null;
		
		try {
			Map map = new HashMap();
			map.put("paramSchool", paramSchool);
			map.put("paramCity", paramCity);
			
			result = ibatis.queryForList("master.selectSchool", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public SchoolModel getSchool(int id){
		SchoolModel result = null;
		
		try {
			Map map = new HashMap();
			map.put("param_id", id);
			result = (SchoolModel)ibatis.queryForList("master.selectSchool", map).get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean InsertSchool(SchoolModel model){		
		boolean result = true;
		
		try{
			ibatis.startTransaction();
			Object resultNo = ibatis.insert("master.insertSchool", model);
			ibatis.commitTransaction();
			
			//System.out.println("Result Insert : " + resultNo);
//			if(resultNo == 1)
//			{result = true;}
		}catch(SQLException e){
			result = false;
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean UpdateSchool(SchoolModel model){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.update("master.updateSchool", model);
			ibatis.commitTransaction();
						
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean DeleteSchool(SchoolModel model){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.delete("master.deleteSchool", model);
			ibatis.commitTransaction();
			
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	//City
	public List selectAllCity(String paramCity){
		List result = null;
		
		try {
			Map map = new HashMap();
			map.put("paramCity", paramCity);
			result = ibatis.queryForList("master.selectCity", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public SchoolModel getCity(int id){
		SchoolModel result = null;
		
		try {
			Map map = new HashMap();
			map.put("param_id", id);
			result = (SchoolModel)ibatis.queryForList("master.selectCity", map).get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean InsertCity(SchoolModel model){		
		boolean result = true;
		
		try{
			ibatis.startTransaction();
			Object resultNo = ibatis.insert("master.insertCity", model);
			ibatis.commitTransaction();
			
			System.out.println("Result Insert : " + resultNo);
//			if(resultNo == 1)
//			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
			result = false;
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean UpdateCity(SchoolModel model){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.update("master.updateCity", model);
			ibatis.commitTransaction();
			
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean DeleteCity(SchoolModel model){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.delete("master.deleteCity", model);
			ibatis.commitTransaction();
						
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	//Group Class
	public List selectAllGroupClass(String paramClassName){
		List result = null;
		try {
			Map map = new HashMap();
			map.put("paramClassName", paramClassName);
			result = ibatis.queryForList("master.selectGroupClass", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public List selectAllClass(){
		List result = null;
		try {
			result = ibatis.queryForList("master.selectClass", "");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public GroupClassModel getLastGroupClassId(){
		GroupClassModel grClassModel = null;
		try {
			grClassModel = (GroupClassModel) ibatis.queryForObject("master.getLastGroupClassId", "");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return grClassModel;
	}	

	//Rere 23 Des 2013
	public GroupClassModel getGroupClassById(Integer groupClassId){
		GroupClassModel grClassModel = null;
		try {
			grClassModel = (GroupClassModel) ibatis.queryForObject("master.selectGroupClassById", groupClassId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return grClassModel;
	}	
	
	public List getGroupClassDetailByClass(Integer groupClassId) {
		List result = null;
		try {
			result = ibatis.queryForList("master.selectGroupClassDetailById", groupClassId);
			System.out.println("Result : " + result.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public List getGroupClassDetail(Integer groupClassId) {
		List result = null;
		try {
			result = ibatis.queryForList("master.getGroupClassDetail", groupClassId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
//	public boolean insertGroupClass(String groupClassName, Integer groupClassWeight, String usrupd){
//		boolean result = true;
//		
//		try {
//			ibatis.startTransaction();
//			HashMap map = new HashMap();
//			map.put("groupClassName", groupClassName);
//			map.put("groupClassWeight", groupClassWeight);
//			map.put("usrupd", usrupd);
//			ibatis.insert("master.insertGroupClass", map);
//			ibatis.commitTransaction();
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//			result = false;
//		} finally {
//			try {
//				ibatis.endTransaction();
//			} catch (Exception e2) {
//				// TODO: handle exception
//				e2.printStackTrace();
//			}
//		}
//		return result;
//	}
	
	//Rere 24 Des 2013
	public boolean insertGroupClass(GroupClassModel model){
		boolean result = true;
		
		try {
			ibatis.startTransaction();			
			ibatis.insert("master.insertGroupClass", model);
			ibatis.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return result;
	}
	
//	public boolean insertGroupClassDetail(Integer groupClassId, Integer classId, Integer groupClassDetWeight, String usrupd){
//		boolean result = true;
//		
//		try {
//			ibatis.startTransaction();
//			HashMap map = new HashMap();
//			map.put("groupClassId", groupClassId);
//			map.put("classId", classId);
//			map.put("groupClassDetWeight", groupClassDetWeight);
//			map.put("usrupd", usrupd);
//			ibatis.insert("master.insertGrClassDetail", map);
//			ibatis.commitTransaction();
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//			result = false;
//		} finally {
//			try {
//				ibatis.endTransaction();
//			} catch (Exception e2) {
//				// TODO: handle exception
//				e2.printStackTrace();
//			}
//		}
//		return result;
//	}
	
	//Rere 24 Des 2013
	public boolean insertGroupClassDetail(GroupClassModel model){
		boolean result = true;
		
		try {
			ibatis.startTransaction();			
			ibatis.insert("master.insertGrClassDetail", model);
			ibatis.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return result;
	}
	
	//Rere 24 Des 2013
	public boolean updateGroupClass(GroupClassModel model){
		boolean result = true;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.update("master.updateGroupClass", model);
			ibatis.commitTransaction();
						
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean deleteGroupClass(Integer groupClassId){
		int result = 0;
		try {
			result = ibatis.delete("master.deleteGrClassDetail", groupClassId);
			try {
				result = ibatis.delete("master.deleteGroupClass",groupClassId);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		if(result == 1){
			return true;
		}else{
			return false;
		}
	}

	//Rere 24 Des 2013
	public boolean DeleteGroupClassDetail(Integer groupClassId){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.delete("master.deleteGrClassDetail", groupClassId);
			ibatis.commitTransaction();
			
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	//Job
	public List selectAllJob(String jobId,String desc){
		System.out.println("MASUKKKK");
		List result=null;
		
		try {
			Map map = new HashMap();
			map.put("paramJobId", jobId);
			map.put("paramDescription", desc);
			result=ibatis.queryForList("master.selectAllJob", map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();                                                              
		}
		return result;
	}
	
	public boolean InsertJob(SchoolModel model){		
		boolean result;
		
		try{
			ibatis.startTransaction();
			this.ibatis.insert("master.insertJob", model);
			ibatis.commitTransaction();
			result=true;

		}catch(SQLException e){
			e.printStackTrace();
			result=false;
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean UpdateJob(SchoolModel model){
		boolean result = true;
		try {	
			ibatis.startTransaction();
			int resultNo = this.ibatis.update("master.updateJob", model);			
			ibatis.commitTransaction();
			
			if(resultNo == 1)
			{result = true;}
			//result=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			result = false;
			e.printStackTrace();
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	public boolean DeleteJob(SchoolModel model){		
		boolean result = false;
		
		try{
			ibatis.startTransaction();
			int resultNo = ibatis.delete("master.deleteJob", model);
			ibatis.commitTransaction();
						
			if(resultNo == 1)
			{result = true;}
		}catch(SQLException e){
			e.printStackTrace();
			result = false;
		}finally{
			try{
				ibatis.endTransaction();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		return result;
	}
}
